#import "gTxtEditView.h"

@implementation gTxtEditTextView

- (id)initWithFrame:(CGRect)rect
{
	if(( self == [ super initWithFrame: rect ]) != nil)
		{
			[ self setDelegate: self ];
		}
	return self;
}



@end


@implementation gTxtEditView

- (id)initWithFrame:(CGRect)rect
{
	if(( self == [ super initWithFrame: rect ]) != nil)
		{
			topBar = [ [ UINavigationBar alloc ] initWithFrame: CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, 45.0f) ];
			[ topBar setDelegate: self ];
				[ topBar showLeftButton:@"KB Hide" withStyle: 2 rightButton:@"Save" withStyle: 0 ];
			topTitle = [ [ UINavigationItem alloc ] initWithTitle:@"gTxtEdit" ];
			[ topBar pushNavigationItem: topTitle ];
			[ topBar enableAnimation ];	
		
			bottomBar = [ [ UINavigationBar alloc ] initWithFrame: CGRectMake(rect.origin.x, rect.origin.y + (rect.size.height - 45), rect.size.width, 45.0f) ];
			[ bottomBar setDelegate: self ];
			[ bottomBar showButtonsWithLeftTitle:@"New" rightTitle:@"Open" leftBack:NO ];
			bottomTitle = [ [ UINavigationItem alloc ] initWithTitle:@"" ];
			[ bottomBar pushNavigationItem: bottomTitle ];
			[ bottomBar enableAnimation ];	
			
			fileView = [ [ gTxtEditTextView alloc ] initWithFrame: CGRectMake(rect.origin.x, rect.origin.y + 45, rect.size.width, rect.size.height - 91) ];
			[ fileView setDelegate: self ];
	

			[ self addSubview: topBar ];
			[ self addSubview: fileView ];
			[ self addSubview: bottomBar ];

			isEditing = FALSE;
			saved = FALSE;
			currentFile = nil;
		}
	return self;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{

	[ fileView setFrame: CGRectMake(0.0f, 45.0f, 320.0f, 230.0f) ];
	isEditing = TRUE;
}


- (void)textViewDidEndEditing:(UITextView *)textView
{
	[ fileView setFrame: CGRectMake(0.0f, 45.0f, 320.0f, [self bounds].size.height - 91) ];
	isEditing = FALSE;
}


- (void)loadFile:(NSString*)file
{
	currentFile = [ [ NSString alloc ] 
			initWithData: [ [ NSFileManager defaultManager ] contentsAtPath: file ] encoding:NSUTF8StringEncoding ];
			
	saved = FALSE;
	[ fileView setText: currentFile ];
	[ bottomTitle setTitle: [ file lastPathComponent ] ];
}

- (void)reset
{
	[ fileView setText: @"" ];
	[ bottomTitle setTitle:@"Untitled.txt" ];
	saved = FALSE;
	currentFile = nil;
}

- (void)saveToFile:(NSString*)file
{
	[[fileView text] 
	   writeToFile: file 
	   atomically: NO 
	   encoding: NSMacOSRomanStringEncoding
	   error: nil];
	saved = TRUE;
	currentFile = [ file copy ];
	[ bottomTitle setTitle: [ file lastPathComponent ] ];
}

- (void)save
{
	[[fileView text]
	    writeToFile: currentFile
	    atomically: NO 
	    encoding: NSMacOSRomanStringEncoding
	    error: nil];
	saved = TRUE;
}


- (void)dealloc
{
	[ fileView release ];
	[ currentFile release ];
	[ bottomTitle release ];
	[ topTitle release ];
	[ bottomBar release ];
	[ topBar release ];
	[ super dealloc ];
}

- (void)navigationBar:(UINavigationBar*)bar buttonClicked:(int)button
{
	if(bar == bottomBar)
		{
			if(button == 1)
				{
					[ self reset ];
				}
			else
				{
					[fileView resignFirstResponder];
					loadSheet = [ [ UIAlertSheet alloc ] init ];
					[ loadSheet setTitle:@"Load File" ];
					[ loadSheet setBodyText:@"Please enter a file to load. Make sure you have saved any changes to your current file before loading a new one." ];
					[ loadSheet setContext:@"_loadSheet" ];
					[ loadSheet addButtonWithTitle:@"Load" ];
					[ loadSheet addButtonWithTitle:@"Cancel" ];
					[ loadSheet setDelegate: self ];
					[ loadSheet setNumberOfRows: 1 ];
					[ loadSheet addTextFieldWithValue:nil label:@"ex. /var/Untitled.txt" ];
					[ loadSheet popupAlertAnimated: YES ];
				}
		}
	else
		{
			if(button == 1)
				{
					[fileView resignFirstResponder];
				
				}
			else
				{
					if(saved)
						{
							[ self save ];
						}
					else
						{
							[fileView resignFirstResponder];
							saveSheet = [ [ UIAlertSheet alloc ] init ];
							[ saveSheet setTitle:@"Save File" ];
							[ saveSheet setBodyText:@"Please enter a location to save to." ];
							[ saveSheet setContext:@"_saveSheet" ];
							[ saveSheet addButtonWithTitle:@"Save" ];
							[ saveSheet addButtonWithTitle:@"Cancel" ];
							[ saveSheet setDelegate: self ];
							[ saveSheet setNumberOfRows: 1 ];
							[ saveSheet addTextFieldWithValue:nil label:@"ex. /var/Untitled.txt" ];
							[ saveSheet popupAlertAnimated: YES ];
						}
				}
		}
}

- (void)alertSheet:(UIAlertSheet*)sheet buttonClicked:(int)button
{
	if(sheet == saveSheet)
		{
			if(button == 1)
				{
					[ self saveToFile: [ [ sheet textField ] text ] ];
					saved = TRUE;
				}
		}
	else if(sheet == loadSheet)
		{
			if(button == 1)
				{
					[ self loadFile: [ [ sheet textField ] text ] ];
				}
		}
	[ sheet dismiss ];
	[ fileView setFrame: CGRectMake(0.0f, 45.0f, 320.0f, [self bounds].size.height - 91) ];
	isEditing = FALSE;
}



@end